import { login, getMenu, getInfo } from '@/api/user'
import * as conkieCRUD from '@/utils/auth'
import { constantRoutes, resetRouter } from '@/router'
import { navs } from '../../router/navs'

const getDefaultState = () => {
  return {
    token: conkieCRUD.getUserToken(),
    name: '',
    userId: conkieCRUD.getUserId(),
    trueName: '',
    userState: 0,
    isHaveMenu: false,
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif' // 头像地址
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_USER_ID: (state, userId) => {
    state.userId = userId
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_TRUE_NAME: (state, trueName) => {
    state.trueName = trueName
  },
  SET_USER_STATE: (state, userState) => {
    state.userState = userState
  },
  SET_IS_HAVE_MENU: (state, isHaveMenu) => {
    state.isHaveMenu = isHaveMenu
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ user_name: username.trim(), user_password: password.trim() }).then(response => {
        const { data } = response
        commit('SET_USER_ID', data.userId)
        commit('SET_TOKEN', data.userToken)
        conkieCRUD.setUserId(data.userId)
        conkieCRUD.setUserToken(data.userToken)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user info
  getInfo({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      Promise.all([
        getInfo(state.userId, state.userToken),
        dispatch('getMenu')
      ]).then(([info]) => {
        const { data } = info

        commit('SET_NAME', data.userName)
        commit('SET_TRUE_NAME', data.userTrueName)
        commit('SET_USER_STATE', data.userState)
        commit('SET_AVATAR', data.headImgUrl)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user menu
  getMenu({ commit, state }) {
    return new Promise((resolve, reject) => {
      getMenu(state.userId).then(response => {
        const { data } = response

        // 修改菜单路由
        const navList = navs(data.menuList)
        navList.forEach(element => {
          constantRoutes.splice(constantRoutes.length - 1, 0, element)
        })
        resetRouter()
        commit('SET_IS_HAVE_MENU', true)

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ dispatch }) {
    return new Promise((resolve) => {
      dispatch('resetCookie').then(() => {
        constantRoutes.splice(3, constantRoutes.length - 4)
        resetRouter()
        resolve()
      })
    })
  },

  // remove token
  resetCookie({ commit }) {
    return new Promise(resolve => {
      conkieCRUD.removeUserToken()
      conkieCRUD.removeUserId()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

